package com.drone.drone_server.controller;

import com.drone.drone_server.api.ApiResponse;
import com.drone.drone_server.dao.PersonPositionDao;
import com.drone.drone_server.entity.PersonPosition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private PersonPositionDao personPositionDao;

    @PutMapping("/login")
    public ApiResponse login(@RequestParam String personPhoneNumber,
                             @RequestParam String password) {
        PersonPosition user = personPositionDao.selectById(Long.parseLong(personPhoneNumber));
        if (user == null) return ApiResponse.fail("用户不存在");
        if (!user.getPassword().equals(password)) return ApiResponse.fail("密码错误");
        return ApiResponse.success(user);
    }

    @PutMapping("/register")
    public ApiResponse register(@RequestParam String personPhoneNumber,
                                @RequestParam String personName,
                                @RequestParam String position, // 前端传入 "security" 或 "leader"
                                @RequestParam String password) {
        // 将字符串转换为枚举
        PersonPosition.Position enumPosition;
        try {
            enumPosition = PersonPosition.Position.valueOf(position);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fail("无效的职位类型");
        }

        PersonPosition newUser = new PersonPosition();
        newUser.setPersonPhoneNumber(Long.parseLong(personPhoneNumber));
        newUser.setPersonName(personName);
        newUser.setPosition(enumPosition);
        newUser.setPassword(password);

        int res = personPositionDao.insert(newUser);
        return res > 0 ? ApiResponse.success("注册成功") : ApiResponse.fail("注册失败");
    }

    @PutMapping("/update")
    public ApiResponse updateInfo(@RequestParam String personPhoneNumber,
                                  @RequestParam String personName,
                                  @RequestParam String position, // 前端传入 "security" 或 "leader"
                                  @RequestParam String password) {
        // 将字符串转换为枚举
        PersonPosition.Position enumPosition;
        try {
            enumPosition = PersonPosition.Position.valueOf(position);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fail("无效的职位类型");
        }

        PersonPosition user = new PersonPosition();
        user.setPersonPhoneNumber(Long.parseLong(personPhoneNumber));
        user.setPersonName(personName);
        user.setPosition(enumPosition);
        user.setPassword(password);

        int res = personPositionDao.updateById(user);
        return res > 0 ? ApiResponse.success("更新成功") : ApiResponse.fail("更新失败");
    }

    @GetMapping("/info")
    public ApiResponse getInfo(@RequestParam String personPhoneNumber) {
        PersonPosition user = personPositionDao.selectById(Long.parseLong(personPhoneNumber));
        if(user==null)return ApiResponse.fail("用户不存在");
        else {
            user.setPassword("");
            return ApiResponse.success(user);
        }
    }

}
